package dao;

import java.util.LinkedList;
import java.util.List;
import entities.CreditCard;
import entities.Customer;

import relations.Has;

public class HasDAO 
{
	private static final HasDAO instance = new HasDAO();
	private List<Has> has;
	
	private HasDAO(){
		has = new LinkedList<Has>();
		Has cus = null;
		
		cus = has.newInstance("jaime", "Jaime", "Vazquez", "ja@j.com", 1L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		has.add(cus);
		cus = has.newInstance("jaime1", "Jaime", "Vazquez", "ja@j.com", 2L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		has.add(cus);
	}
	
	public static HasDAO getInstance() {
		return instance;
	}

	public synchronized Has findHas(String creditCardNum, String userID) {
		Has result = null;
		
		for (Has s : has){
			if (s.getCreditCardNum().equals(creditCardNum) && s.getUserID().equals(userID)){
				result = s;
			}
		}
		return result;
	}

	public synchronized void addHas(Has obj) throws RecordDuplicateException{
		if (findHas(obj.getCreditCardNum(), obj.getUserID()) != null){
			throw new RecordDuplicateException("Has with credit card number: " + obj.getCreditCardNum()
					+ "and user id: " + obj.getUserID() + " is already in the db.");
		}
		else {
			has.add(obj);
		}
	}
}
